﻿using AutoMapper;
using HRSolutionSystem.Application.Contracts.Persistence;
using HRSolutionSystem.Application.Exceptions;
using HRSolutionSystem.Application.Features.LeaveType.Queries.GetAllLeaveTypes;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRSolutionSystem.Application.Features.LeaveType.Queries.GetLeaveTypeDetails
{
    public class GetLeaveTypeDetailsQueryHandler : IRequestHandler<GetLeaveTypeDetailsQuery,
        List<LeaveTypeDetailsDto>>
    {
        private readonly IMapper _mapper;
        private readonly ILeaveTypeRepository _leaveTypeRepository;

        public GetLeaveTypeDetailsQueryHandler(IMapper mapper, ILeaveTypeRepository leaveTypeRepository)
        {
            this._mapper = mapper;
            this._leaveTypeRepository = leaveTypeRepository;
        }
        public async Task<List<LeaveTypeDetailsDto>> Handle(GetLeaveTypeDetailsQuery request, CancellationToken cancellationToken)
        {
            //1 Query the database
            var leaveTypeDetails = await _leaveTypeRepository.GetByIdAsync(request.id);
            if (leaveTypeDetails == null)
            {
                throw new NotFoundException(nameof(leaveTypeDetails),request.id);
            }
            //2 convert data objects to DTO objects
            var data = _mapper.Map<List<LeaveTypeDetailsDto>>(leaveTypeDetails);
            //3 return list of DTO object
            return data;
        }
    }
}
